package name.sraynal.coodiary;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import name.sraynal.coodiary.file.analyzer.ComputerFileAnalyzer;
import name.sraynal.coodiary.file.analyzer.IFileAnalyzer;
import name.sraynal.coodiary.file.creator.IOutputCreator;
import name.sraynal.coodiary.file.creator.http.HttpCreator;
import name.sraynal.coodiary.file.creator.mobile.MobileHttpCreator;
import name.sraynal.coodiary.fileprovider.ConfigFileProvider;
import name.sraynal.coodiary.storage.IStorageManager;
import name.sraynal.coodiary.storage.MapStorageManager;
import name.sraynal.coodiary.storage.Quote;

public class CoolDiaryMain {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		IStorageManager storageManager = new MapStorageManager();
		storageManager.createTables();

		// 1. Analyze files

		// File file = new File("C:\\Users\\Seb\\Documents\\xls\\dispatch");
		// List<File> files = new ArrayList<File>();
		// files.addAll(getListFileRecur(file));
		//
		// // old output file analyzer
		// IFileAnalyzer oldOutputFileAnalyzer = new OldOutputFileAnalyzer();
		// Set<Quote> quotes = oldOutputFileAnalyzer.analyze(files);

		// // recent quotes file analyzer
		// File recentDir = new
		// File("C:\\Users\\Seb\\Documents\\xls\\res_quotes");
		// List<File> recentFiles = Arrays.asList(recentDir.listFiles());

		// Get config
		ConfigFileProvider configProvider = new ConfigFileProvider(new File(
				args[0]));
		List<File> recentFiles = configProvider.getFiles();
		Set<Quote> quotes = new HashSet<Quote>();

		for (int i = 0; i < recentFiles.size(); i++) {
			IFileAnalyzer computerFileAnalyzer = new ComputerFileAnalyzer(
					configProvider);
			quotes.addAll(computerFileAnalyzer.analyze(recentFiles.get(i)));
		}

		// 2. Store quotes
		// quotes = filterQuotes(quotes, configProvider);
		Iterator<Quote> it = quotes.iterator();

		while (it.hasNext()) {
			storageManager.store(it.next());
		}

		// 3. Create outputs

		IOutputCreator httpOpCreator = new HttpCreator();
		File httpOut = new File(
				"C:\\Users\\Seb\\Documents\\java\\CoolDiary\\out\\http");
		cleanDir(httpOut);
		httpOpCreator.createOutput(httpOut, storageManager.getQuotes());

		IOutputCreator mobileHttpOpCreator = new MobileHttpCreator();
		File mobileOut = new File(
				"C:\\Users\\Seb\\Documents\\java\\CoolDiary\\out\\mobile");
		cleanDir(mobileOut);
		mobileHttpOpCreator.createOutput(mobileOut, storageManager.getQuotes());

		System.out.println("Done.");
	}

	private static void cleanDir(File httpOut) {
		if (httpOut.exists()) {
			httpOut.delete();
		}
		httpOut.mkdir();
	}

	public static List<File> getListFileRecur(File file) {
		List<File> result = new ArrayList<File>();
		List<File> stack = new ArrayList<File>();
		stack.add(file);

		while (!stack.isEmpty()) {
			File current = stack.get(stack.size() - 1);
			stack.remove(current);
			if (current.isDirectory()) {
				stack.addAll(Arrays.asList(current.listFiles()));
			} else if (current.isFile()) {
				result.add(current);
			}
		}
		return result;
	}
}
